import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.AndTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SentDateTerm;
import javax.mail.search.SubjectTerm;


public class CheckResultMail
{
    private String email;
    private String checkEmail;
    private String pwd;
    private final static String CONFIRM_SUBJECT = "你的預訂申請已被確認";
    private boolean isGmail;

    public CheckResultMail(String email, String pwd)
    {
        this.checkEmail = email;
        this.email = email;
        if (email != null && email.indexOf("yahoo.cn") > 0)
        {
	        this.email = "junhehk@yahoo.cn";
            this.pwd = "lovegq";
        }
        isGmail = email.indexOf("gmail.com") > 0;
        if (pwd != null && !"".equals(pwd))
        {
            this.pwd = pwd;
        }
        else
        {
            if (isGmail)
            {
                this.pwd = "99172107";
            }
            else
            {
                this.pwd = "lovegq";
            }
        }
    }
    
    public boolean isBingo()
    {
        Properties props = new Properties();
        if (isGmail)
        {
            props.put("mail.smtp.port", 465);
//            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.auth", "true");  
            props.put("mail.debug", "true");  
            props.put("mail.smtp.socketFactory.port", 465);  
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
            props.put("mail.smtp.socketFactory.fallback", "false");  

        }
        Session session = Session.getDefaultInstance(props);
        try
        {
            Store store;
            if (email.indexOf("163.com") > 0)
            {
	            store = session.getStore("pop3");
                store.connect("pop3.163.com", email, pwd);
            }
            else
            {
	            store = session.getStore("pop3");
                store.connect("pop.mail.yahoo.com.cn", email, pwd);
            }
            Folder folder = store.getFolder("Inbox");
            folder.open(Folder.READ_ONLY);
            
            SentDateTerm startDateTerm = new SentDateTerm(ComparisonTerm.GE, getStartDate());
            Calendar c = Calendar.getInstance();
            SentDateTerm endDateTerm = new SentDateTerm(ComparisonTerm.LT, c.getTime());
            SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm);
            SearchTerm subjectTerm = new SubjectTerm(CONFIRM_SUBJECT);
            
            Message messages[] = folder.search(new AndTerm(dateTerm, subjectTerm));
            folder.close(false);
            store.close();
            if (messages != null && messages.length > 0)
            {
                for (int i = 0; i < messages.length; i++)
                {
                    Message mes = messages[i];
                    Address[] address = mes.getRecipients(Message.RecipientType.TO);
                    if (this.checkEmail.equals(address[0].toString()))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        return false;
    }
    
    private Date getStartDate()
    {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 17);
        c.set(Calendar.MINUTE, 30);
        Calendar now = Calendar.getInstance();
        if (now.getTimeInMillis() <= c.getTimeInMillis())
        {
            c.add(Calendar.HOUR, -24);
        }
        return c.getTime();
    }
}
